package smallTools;

public class TimeSlotUtility {

	// the priority of start hour
	private final int[] hourPriority = { 11, 12, 13, 14, 15, 16, 17, 18, 19,
			20, 21, 10, 9, 8 };
	private SimpleTime[] allTimeSlot;

	public TimeSlotUtility() {
		int nDays = 5;
		int startAt30Minute = 30;
		allTimeSlot = new SimpleTime[nDays * hourPriority.length];
		int timeSlotIndex = 0;

		for (int currentHour : hourPriority) {
			for (int day = 1; day <= nDays; day++) {
				String strDay = SimpleTime.dayIntToString(day);
				allTimeSlot[timeSlotIndex++] = new SimpleTime(strDay,
						currentHour, startAt30Minute);
			}
		}
	}

	private int currentSectionIndex=0;

	public SimpleTime getFirstTimeSlot() {
		currentSectionIndex = 0;
		return allTimeSlot[currentSectionIndex];
	}

	public SimpleTime getNextTimeSlot(int durationConstraint) {
		currentSectionIndex++;
		if (currentSectionIndex >= allTimeSlot.length) {
			return null;
		} else {
			SimpleTime result = allTimeSlot[currentSectionIndex];
			// check the evening times, the end time of the course must not
			// excess 22:30
			while (result.getEndTime(durationConstraint).getHour() > 22) {
				currentSectionIndex++;
				result = allTimeSlot[currentSectionIndex];
			}
			return result;
		}
	}
}
